clear
close all
clc

fntsize=14;
w=500;
h=w*2/(1+sqrt(5));
col=[0;0.5;0.8;1;0.6;0.9]*ones(1,3);
printyes=1;

%% Load data
[data,labels]=xlsread('DataForFiguresTables.xlsx','XS_data','A1:H141');
[T,N]=size(data);
for i=1:N
    assignin('base',labels{1,i},reshape(data(:,i),14,T/14));
end
year=year(1,:)';
LS=LS(:,1);
for i=1:length(year)
    assignin('base',strcat('share_establ_'  ,num2str(year(i))),1*share_establ(:,i));
    assignin('base',strcat('share_empl_'    ,num2str(year(i))),1*share_empl(:,i));
    assignin('base',strcat('share_sales_'   ,num2str(year(i))),1*share_sales(:,i));
    assignin('base',strcat('share_va_'      ,num2str(year(i))),1*share_va(:,i));
    assignin('base',strcat('share_va_firms_',num2str(year(i))),1*share_va_firms(:,i));
end

%% Plot Fig. I: XS distribution 1967 vs. 2012

figure('position',[0 0 w h+10]);
    [haxes,hbar,hline] = plotyy(LS,share_va_1967,LS,share_establ_1967,'bar','plot');
    set(hline,'lineWidth',2,'color',col(1,:))
    set(hbar,'facecolor',0.8*ones(1,3),'edgecolor',[0 0 0],'barwidth',1)
    set(haxes(1),'ycolor',col(2,:),'ylim',[0 0.24],'ytick',(0:0.04:0.24),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize)
    set(haxes(2),'ycolor',col(1,:),'ylim',[0 0.18],'ytick',(0:0.03:0.18),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize,'layer','top')
    grid
    ylabel(haxes(1),'Share of value added','fontsize',fntsize,'color',[0.5 0.5 0.5],'interpreter','latex')
    ylabel(haxes(2),'Share of establishments','fontsize',fntsize,'interpreter','latex')
    title('1967','fontsize',fntsize,'interpreter','latex')
    xlabel('Labor share $\lambda$','interpreter','latex','fontsize',fntsize)
%     lgnd=legend([hline;hbar],{'Share of establishments (right axis)';'Share of value added (left axis)'},'location','ne','fontsize',fntsize,'interpreter','latex');
%     set(lgnd,'color','white');
if printyes==1
    print('../Figures//Fig_LS_Distr_VA_1967.eps','-depsc')
end

figure('position',[0 0 w h+10]);
    [haxes,hbar,hline] = plotyy(LS,share_va_2012,LS,share_establ_2012,'bar','plot');
    set(hline,'lineWidth',2,'color',col(1,:))
    set(hbar,'facecolor',0.8*ones(1,3),'edgecolor',[0 0 0],'barwidth',1)
    set(haxes(1),'ycolor',col(2,:),'ylim',[0 0.24],'ytick',(0:0.04:0.24),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize)
    set(haxes(2),'ycolor',col(1,:),'ylim',[0 0.18],'ytick',(0:0.03:0.18),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize,'layer','top')
    grid
    ylabel(haxes(1),'Share of value added','fontsize',fntsize,'color',[0.5 0.5 0.5],'interpreter','latex')
    ylabel(haxes(2),'Share of establishments','fontsize',fntsize,'interpreter','latex')
    title('2012','fontsize',fntsize,'interpreter','latex')
    xlabel('Labor share $\lambda$','interpreter','latex','fontsize',fntsize)
    lgnd=legend([hline;hbar],{'Share of establishments (right axis)';'Share of value added (left axis)'},'location','ne','fontsize',fntsize,'interpreter','latex');
    set(lgnd,'color','white');
if printyes==1
    print('../Figures//Fig_LS_Distr_VA_2012.eps','-depsc')
end

%% Plot Fig. III: XS distribution 1967 vs. 2012 in numbers, L and VA
figure('position',[0 0 2*w 3*h]);
n=1;
subplot(3,2,n);n=n+1;
    hbar = bar(LS,share_establ_1967);
    set(hbar,'facecolor',col(6,:),'edgecolor',[0 0 0],'barwidth',1)
%     set(gca,'ylim',[0 0.18] ,'ytick',(0:0.03:0.18),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize)
    set(gca,'ylim',[0 0.219],'ytick',(0:0.04:0.20),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize)
    grid
    xlabel('Labor share $\lambda$','interpreter','latex','fontsize',fntsize+4)
    ylabel('{Share of establishments}','fontsize',fntsize+4,'interpreter','latex')
    title('1967','fontsize',fntsize+4,'interpreter','latex')
subplot(3,2,n);n=n+1;
    hbar = bar(LS,share_establ_2012);
    set(hbar,'facecolor',col(5,:),'edgecolor',[0 0 0],'barwidth',1)
%     set(gca,'ylim',[0 0.18] ,'ytick',(0:0.03:0.18),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize)
    set(gca,'ylim',[0 0.219],'ytick',(0:0.04:0.20),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize)
    grid
    xlabel('Labor share $\lambda$','interpreter','latex','fontsize',fntsize+4)
    title('2012','fontsize',fntsize+4,'interpreter','latex')
subplot(3,2,n);n=n+1;
    hbar = bar(LS,share_empl_1967);
    set(hbar,'facecolor',col(6,:),'edgecolor',[0 0 0],'barwidth',1)
%     set(gca,'ylim',[0 0.18] ,'ytick',(0:0.03:0.18),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize)
    set(gca,'ylim',[0 0.219],'ytick',(0:0.04:0.20),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize)
    grid
    xlabel('Labor share $\lambda$','interpreter','latex','fontsize',fntsize+4)
    ylabel('Share of labor input','fontsize',fntsize+4,'interpreter','latex')
subplot(3,2,n);n=n+1;
    hbar = bar(LS,share_empl_2012);
    set(hbar,'facecolor',col(5,:),'edgecolor',[0 0 0],'barwidth',1)
%     set(gca,'ylim',[0 0.18] ,'ytick',(0:0.03:0.18),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize)
    set(gca,'ylim',[0 0.219],'ytick',(0:0.04:0.20),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize)
    grid
    xlabel('Labor share $\lambda$','interpreter','latex','fontsize',fntsize+4)
subplot(3,2,n);n=n+1;
    hbar = bar(LS,share_va_1967);
    set(hbar,'facecolor',col(6,:),'edgecolor',[0 0 0],'barwidth',1)
%     set(gca,'ylim',[0 0.18] ,'ytick',(0:0.03:0.18),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize)
    set(gca,'ylim',[0 0.219],'ytick',(0:0.04:0.20),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize)
    grid
    xlabel('Labor share $\lambda$','interpreter','latex','fontsize',fntsize+4)
    ylabel('Share of value added','fontsize',fntsize+4,'interpreter','latex')
subplot(3,2,n);
    hbar = bar(LS,share_va_2012);
    set(hbar,'facecolor',col(5,:),'edgecolor',[0 0 0],'barwidth',1)
%     set(gca,'ylim',[0 0.18] ,'ytick',(0:0.03:0.18),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize)
    set(gca,'ylim',[0 0.219],'ytick',(0:0.04:0.20),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize)
    grid
    xlabel('Labor share $\lambda$','interpreter','latex','fontsize',fntsize+4)
if printyes==1
    print('../Figures//Fig_LS_Distr_N_Y_L_2012v1967.eps','-depsc')
end

%% Plot Fig. C.1: XS distribution 1967 vs. 2012 of firms
figure('position',[0 0 w h]);
[hbar] = bar(LS,share_va_firms_1967);
set(hbar,'facecolor',0.8*ones(1,3),'edgecolor',[0 0 0],'barwidth',1)
set(gca,'ylim',[0 0.24],'ytick',(0:0.04:0.24),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize)
grid
xlabel('Labor share $\lambda$','interpreter','latex','fontsize',fntsize)
title('1967','fontsize',fntsize,'interpreter','latex')
% lgnd=legend('Share of value added','location','ne','fontsize',fntsize,'interpreter','latex');
% set(lgnd,'color','white');
if printyes==1
    print('../Figures//Fig_LS_Distr_VA_1967_firms.eps','-depsc')
end

figure('position',[0 0 w h]);
[hbar] = bar(LS,share_va_firms_2012);
set(hbar,'facecolor',0.8*ones(1,3),'edgecolor',[0 0 0],'barwidth',1)
set(gca,'ylim',[0 0.24],'ytick',(0:0.04:0.24),'xlim',[0 1.4],'xtick',(0:0.2:1.4),'ticklabelinterpreter','latex','fontsize',fntsize)
grid
xlabel('Labor share $\lambda$','interpreter','latex','fontsize',fntsize)
title('2012','fontsize',fntsize,'interpreter','latex')
lgnd=legend('Share of value added','location','ne','fontsize',fntsize,'interpreter','latex');
set(lgnd,'color','white');
if printyes==1
    print('../Figures//Fig_LS_Distr_VA_2012_firms.eps','-depsc')
end


